<template>
  <div class="login_container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="logo">
        <img src="../assets/logo.png" alt="" />
      </div>
      <!-- 登录表单 -->

      <el-form class="login-info" :model="form" status-icon :rules="rules" ref="form">
        <el-form-item prop="username">
          <el-input
            prefix-icon="el-icon-user-solid"
            v-model="form.username"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            type="password"
            autocomplete="off"
            prefix-icon="el-icon-lock"
            v-model="form.password"
          ></el-input>
        </el-form-item>
        <!-- 按钮区域 -->
        <el-form-item class="btn-group">
          <el-button type="primary" @click="submitForm('form')">登录</el-button>
          <el-button type="info" @click="resetForm('form')">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data () {
    return {
      form: {
        password: '',
        username: ''
      },
      // 表单的验证规则对象
      rules: {
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 10, message: '密码的长度应为3-10个字符' }
        ],
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' }
        ]
      }
    }
  },
  created () {},
  computed: {},
  methods: {
    // 提交表单
    submitForm (formName) {
      this.$refs[formName].validate(async (valid) => {
        if (valid) {
          // 解构赋值 await async化简promise
          const { data: res } = await this.$http.post('login', this.form)
          if (res.meta.status !== 200) {
            this.$message.error(res.meta.msg)
            return false
          }
          this.$message.success('登录成功')
          // 将token 存到sessionStorage中
          console.log(res.data.token)
          window.sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        } else {
          this.$message.error('登录失败')
          return false
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>
<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  .login-box {
    height: 300px;
    width: 450px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 3px;
    .logo {
      height: 130px;
      width: 130px;
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 10px;
      box-shadow: 0 0 10px #ddd;
      position: absolute;
      left: 50%;
      transform: translate(-50%, -50%);
      background-color: #fff;
      img {
        height: 100%;
        width: 100%;
        border-radius: 50%;
        background-color: #eee;
      }
    }
    .login-info {
      padding: 20px;
      // border: 1px solid red;
      width: 100%;
      position: absolute;
      bottom: 0;
      box-sizing: border-box;
      .btn-group {
        float: right;
      }
    }
  }
}
</style>
